package com.news.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.news.entity.User;
import com.news.util.JDBCUtil;

/**
 * 用户数据访问类
 *
 */
public class UserDao {
	/**
	 * 获取用户列表
	 * @return 用户列表
	 */
	JDBCUtil util = JDBCUtil.getInstance();
	public List<User> selectList(){
		try {
			String sql = "select * from tb_user order by createTime DESC";
			ResultSet rs = util.query(sql);
			List<User> users = new ArrayList<User>();
			while(rs.next()){
				int id = rs.getInt("id");
				String username = rs.getString("username");
				String password = rs.getString("password");
				String role = rs.getString("role");
				int sex = rs.getInt("sex");
				int age = rs.getInt("age");
				String createTime = rs.getString("createTime");
				User user = new User();
				user.setId(id);
				user.setUsername(username);
				user.setPassword(password);
				user.setRole(role);
				user.setSex(sex);
				user.setAge(age);
				user.setCreateTime(createTime);
				users.add(user);
			}
			util.close();
			return users;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 新增用户
	 * @param user 用户信息
	 * @return 是否成功
	 */
	public boolean insert(User user){
		// 创建一个新用户，就会自动获取当前系统的时间
		Date queueDate = new Date();
		SimpleDateFormat queueDateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String createTime = queueDateFormat.format(queueDate);
		
		String sql="insert into tb_user(username,password,role,sex,age,createTime)"
				+ " values(?,?,?,?,?,?)";
		List<Object> params=new ArrayList<>();
		params.add(user.getUsername());
		params.add(user.getPassword());
		params.add(user.getRole());
		params.add(user.getSex());
		params.add(user.getAge());
		params.add(createTime);
		boolean flag= util.update(sql, params);
		util.close();
		return flag;
	}
	
	/**
	 * 根据id查询用户数据
	 * @param id 主键
	 * @return 用户信息
	 */
	public User selectById(int id){
		try {
			String sql ="select * from tb_user where id=?";
		    List<Object> params =new  ArrayList<>();
		    params.add(id);
		    ResultSet rs =util.query(sql, params);
			if (rs.next()) {
				String username=rs.getString("username");
				String password = rs.getString("password");
				String role = rs.getString("role");
				int sex = rs.getInt("sex");
				int age = rs.getInt("age");
				User user= new User();
				user.setId(id);
				user.setUsername(username);
				user.setPassword(password);
				user.setRole(role);
				user.setSex(sex);
				user.setAge(age);
				return user;
			}
			return null;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}finally {
			util.close();
		}
	}
	
	/**
	 * 修改用户密码
	 * @param id
	 * @param password
	 * @return
	 */
	public boolean modifyPassword(int id,String password){
		JDBCUtil util=JDBCUtil.getInstance();
		String sql="update tb_user set password=? where id=?";
		List<Object> params=new ArrayList<>();
		params.add(password);
		params.add(id);
		boolean flag=util.update(sql, params);
		util.close();
		return flag;
	}
	
	/**
	 * 根据id更新用户
	 * @param brand 用户信息
	 * @return 是否成功
	 */
	public boolean updateUser(User user) {
		String sql = "update tb_user set username=?,password=?,role=?,sex=?,age=?"
				+ " where id=?";
		List<Object> params=new ArrayList<>();
		params.add(user.getUsername());
		params.add(user.getPassword());
		params.add(user.getRole());
		params.add(user.getSex());
		params.add(user.getAge());
		params.add(user.getId());
		boolean flag= util.update(sql, params);
		util.close();
		return flag;
	}
	
	/**
	 * 根据id删除数据
	 * @param id 用户id
	 * @return 是否成功
	 */
	public boolean deleteUser(int id) {
		String sql = "delete from tb_user where id = ?";
		List<Object> params = new ArrayList<>();
		params.add(id);
		boolean flag = util.update(sql, params);
		util.close();
		return flag;
	}
	
	
	
	/**
	 * 用户注册  
	 * @param user
	 * @return
	 */
	public boolean registername(User user){
		// 创建一个新用户，就会自动获取当前系统的时间
		Date queueDate = new Date();
		SimpleDateFormat queueDateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String createTime = queueDateFormat.format(queueDate);
//		JDBCUtil util=JDBCUtil.getInstance();
		String sql="insert into tb_user(username,password,role,sex,age,createTime)"
				+ " values(?,?,?,?,?,?)";
		List<Object> params=new ArrayList<>();
		params.add(user.getUsername());
		params.add(user.getPassword());
		params.add(user.getRole());
		params.add(user.getSex());
		params.add(user.getAge());
		params.add(createTime);
		boolean flag =util.update(sql, params);
		util.close();
		return flag;
	}

	/**
	 * 用户登录
	 * @param username
	 * @param password
	 * @return
	 */
	public User isloginsuccess(String username,String password){
//		JDBCUtil util =JDBCUtil.getInstance();
		String sql ="select  * from tb_user where username=? and password=?";
		List<Object> params= new ArrayList<>();
		params.add(username);
		params.add(password);
		try {
			ResultSet rs =util.query(sql, params);
			while(rs.next()){
				User user =new User();
				user.setId(rs.getInt("id"));
				user.setUsername(rs.getString("username"));
				user.setPassword(rs.getString("password"));
				user.setRole(rs.getString("role"));
				user.setSex(rs.getInt("sex"));
				user.setAge(rs.getInt("age"));
				user.setCreateTime(rs.getString("createTime"));
				return user;
			}
		} catch (Exception e) {
		
			e.printStackTrace();
		}
		return null;
	}
	
	public boolean updatePassword(User user){
		JDBCUtil util =JDBCUtil.getInstance();
		String sql ="update tb_user set password=?  where username=?";
		List<Object> params =new ArrayList<>();
		params.add(user.getPassword());
		params.add(user.getUsername());
		boolean flag =util.update(sql, params);
		System.out.println(sql);
		util.close();
		return flag;
		
	}
}
